package others.swing.p011;

/*学生成绩管理系统 版本T1.0学生课程修改界面*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class CSCourse extends JFrame implements ActionListener {
    Label t1, t2, t3, t4;
    JTextField f1, f2, f3, f4;
    JButton b;
    String sql = "";
    DBAccess db = new DBAccess("SMSystem", "", "");//数据库操作
    CheckEmptyFour cktf;//判断输入是否为空

    public CSCourse() {
        t1 = new Label("修改学生：");
        t2 = new Label("课程一：");
        t3 = new Label("课程二：");
        t4 = new Label("课程三：");
        f1 = new JTextField(12);
        f2 = new JTextField(6);
        f3 = new JTextField(6);
        f4 = new JTextField(6);
        b = new JButton("修改");
        setLayout(null);

        //设置标签大小和所在位置
        t1.setBounds(40, 30, 80, 50);
        t2.setBounds(40, 90, 80, 50);
        t3.setBounds(40, 150, 80, 50);
        t4.setBounds(40, 210, 80, 50);
        b.setBounds(350, 210, 100, 50);
        f1.setBounds(130, 30, 100, 35);
        f2.setBounds(130, 90, 80, 35);
        f3.setBounds(130, 150, 80, 35);
        f4.setBounds(130, 210, 80, 35);
        //设置标签内的字体
        t1.setFont(new Font("楷体", 1, 15));//字体大小
        t2.setFont(new Font("楷体", 1, 15));//字体大小
        t3.setFont(new Font("楷体", 1, 15));//字体大小
        t4.setFont(new Font("楷体", 1, 15));//字体大小
        f1.setFont(new Font("楷体", 1, 15));//字体大小
        f2.setFont(new Font("楷体", 1, 15));//字体大小
        f3.setFont(new Font("楷体", 1, 15));//字体大小
        f4.setFont(new Font("楷体", 1, 15));//字体大小

        b.setFont(new Font("楷体", 1, 15));//字体大小

        t1.setForeground(Color.BLUE);//字体颜色
        t2.setForeground(Color.BLUE);//字体颜色
        t3.setForeground(Color.BLUE);//字体颜色
        t4.setForeground(Color.BLUE);//字体颜色
        b.setForeground(Color.BLUE);//字体颜色
        add(t1);
        add(t2);
        add(t3);
        add(t4);
        add(f1);
        add(f2);
        add(f3);
        add(f4);
        add(b);
        b.addActionListener(this);
        setSize(500, 350);
        setVisible(true);
    }

    //修改学生成绩
    public int changegrade(String sid, String s1, String s2, String s3) {
        String id[];    //学号
        int i = 0, j = 0, a = 0;        //a=0时是未设置学生信息或者没设置成功
        try {
            db.dbcon();
            sql = "SELECT * FROM student";
            db.dbSelect(sql);
            while (db.rs.next()) {
                j++;
            }
            id = new String[j];
            db.rs.beforeFirst();   //游标返回开头位置
            for (i = 0; i < j; i++) {
                db.rs.next();
                id[i] = db.rs.getString(2);
            }
            for (i = 0; i < j; i++) {
                if (sid.equals(id[i])) {
                    sql = "update student set SCourse1='" + s1 + "' where SID='" + sid + "'";
                    db.dbUpdate(sql);   //执行更新
                    sql = "update student set SCourse2='" + s2 + "' where SID='" + sid + "'";
                    db.dbUpdate(sql);   //执行更新
                    sql = "update student set SCourse3='" + s3 + "' where SID='" + sid + "'";
                    db.dbUpdate(sql);   //执行更新
                    a = 1;    //修改成功
                }
            }
            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
        return a;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        cktf = new CheckEmptyFour(f1, f2, f3, f4);
        if (cktf.check(0) != false) {
            if (e.getSource() == b) {
                if (changegrade(f1.getText(), f2.getText(), f3.getText(), f4.getText()) == 1) {
                    JOptionPane.showMessageDialog(null, "修改成功！", "友情提示", 1);
                } else {
                    JOptionPane.showMessageDialog(null, "读取数据库出错！", "友情提示", 1);
                }
            }
        }
    }
}